package com.woniu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.entity.PageResult;
import com.woniu.entity.Result;
import com.woniu.pojo.Item;
import com.woniu.vo.SearchCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.woniu.pojo.Package;
import com.woniu.service.IPackageService;

import java.util.List;

/**
 * <p>
 * 服务包表 前端控制器
 * </p>
 *
 * @author YMY
 * @since 2022-10-12
 */
@Api(tags = "服务包")
@RestController
@RequestMapping("/package")
    public class PackageController extends BaseController{

        @Autowired
        private IPackageService packageService;


        @ApiOperation("查找所有服务包")
        @GetMapping
        public Result queryAll(){
            List<Package> packageList = packageService.list();

            return toDataResult(packageList) ;

        }

        @ApiOperation("新增服务包")
        @PostMapping
        public Result add(@RequestBody Package packages){

            return toResult(packageService.save(packages));

        }

        @ApiOperation("根据id删除服务包")
        @DeleteMapping("/{id}")
        public Result deleteById(@PathVariable Integer id){

            return toResult(packageService.removeById(Long.valueOf(id+"")));
        }

        @ApiOperation("根据id修改服务包")
        @PutMapping
        public Result modifyById(@RequestBody Package packages){

            return toResult(packageService.updateById(packages));
        }


        @ApiOperation("根据条件分页查询服务包")
        @PostMapping("/search/{page}/{size}")
        public Result search(@PathVariable int page, @PathVariable int size, @RequestBody SearchCriteria searchCriteria){
            IPage<Package> p=new Page<Package>(page,size);
            QueryWrapper<Package> wrapper=new QueryWrapper<>();
            if(searchCriteria.getName()!=null)wrapper.like("name",searchCriteria.getName());
            if(searchCriteria.getStartTime()!=null)wrapper.gt("create_time",searchCriteria.getStartTime());
            if(searchCriteria.getEndTime()!=null)wrapper.lt("create_time",searchCriteria.getEndTime());
            if(searchCriteria.getOther()!=null&&searchCriteria.getOther().size()>0){
                searchCriteria.getOther().forEach((key,value)->{
                wrapper.eq(key,value);
                });
            }
            if(searchCriteria.getOtherLike()!=null&&searchCriteria.getOtherLike().size()>0){
                searchCriteria.getOtherLike().forEach((key,value)->{
                wrapper.like(key,value);
                });
            }

            p = packageService.page(p, wrapper);
            PageResult pageResult=new PageResult();
            pageResult.setData(p.getRecords());
            pageResult.setTotal(p.getTotal());

            return toDataResult(pageResult);
        }

        @ApiOperation("根据服务包ID查询所有的服务项目")
        @GetMapping("/queryItem/{packageId}")
        public Result queryItemByPackage(@PathVariable Long packageId){
            List<Item> itemList = packageService.queryItemByPackage(packageId);
            return toDataResult(itemList) ;

        }
    }

